/*
 * @Description: 调试api仓库
 * @Autor: huoyou
 * @Date: 2022-01-11 11:10:39
 * @LastEditTime: 2022-01-11 22:28:44
 */
const fs = require("fs");
const path = require("path");
const http = require("http");
const axios = require("axios");

var params = {
  appid: "1",
  siteid: 10001,
  sharesiteid: 10001,
  clientid: 1,
  modules: "common:2",
  areas: ",,",
  type: "android",
  contentid: "431411",
};
var url = "https://m-api.szdute.cn/v2/article";

refresh();

function writeFile() {
  let file = path.resolve(__dirname, "../index.html");
  console.log(`🚀 ~ writeFile ~ file`, file);
  // 异步写入数据到文件
  fs.writeFile(
    file,
    JSON.stringify(this.content, null, 4),
    { encoding: "utf8" },
    (err) => {
      console.log(`🚀 ~ writeFile ~ err`, err);
    }
  );
  fs.writeFile(path, data, "utf8", (err) => {
    if (err) {
      console.log(err.message);
    } else {
      resolve(true);
    }
  });
}

function generateFile(data) {
  let file = path.resolve(__dirname, "../index.html");
  return new Promise((resolve, reject) => {
    fs.writeFile(file, data, "utf8", (err) => {
      if (err) {
        errorLog(err.message);
        reject(err);
      } else {
        resolve(true);
      }
    });
  });
}

function refresh() {
  axios.get(url, { params }).then((res) => {
    if (res.data.state) {
      content = res.data.data.common.content;
      // content = res.data.data.common.content.replace(
      //   /https:\/\/m-api\.dutenews\.com/g,
      //   "v5"
      // );
      generateFile(content);
      console.log("生成成功");
    }
  });
}
